package com.tianqu.tms.www.finance.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.TransferFinanceDetailDto;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.entity.TransferFinance;
import com.tianqu.tms.www.finance.actionform.CarProxyPaySearchInputForm;
import com.tianqu.tms.www.finance.viewhelper.CarProxyPaySearchInputViewHelper;

public class CarProxyPaySearchResultAction extends TmsAction {

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws Exception {
		CarProxyPaySearchInputForm inputForm = (CarProxyPaySearchInputForm) form;

		String serial = inputForm.getSerial();
		String vin = inputForm.getVin();
		String licensePlate = inputForm.getLicensePlate();
		CarProxyPaySearchInputViewHelper view = new CarProxyPaySearchInputViewHelper();
		int i = 0;
		if (!StringUtil.isNullOrBlank(serial))
			i++;
		if (!StringUtil.isNullOrBlank(vin))
			i++;
		if (!StringUtil.isNullOrBlank(licensePlate))
			i++;

		if (i == 0 || i > 2) {
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			setErrorsToRequest(request, TmsConstants.U0040, null); // 必须输入一个查询条件。
			return mapping.getInputForward();
		}

		//
		TransferAcceptedInfo accept = new TransferAcceptedInfo();
		
		if (!StringUtil.isNullOrBlank(vin) && accept.isTransferCompleted(vin, 1,"1".equals(inputForm.getSaveType()))) {
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			setErrorsToRequest(request, TmsConstants.U1602, null);
			return mapping.getInputForward();
		}
		
		
		if (!StringUtil.isNullOrBlank(serial) && accept.isTransferCompleted(serial, 2,"1".equals(inputForm.getSaveType()))) {
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			setErrorsToRequest(request, TmsConstants.U1602, null);
			return mapping.getInputForward();
		}
		
		if (!StringUtil.isNullOrBlank(licensePlate) && accept.isTransferCompleted(licensePlate, 3,"1".equals(inputForm.getSaveType()))) {
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			setErrorsToRequest(request, TmsConstants.U1602, null);
			return mapping.getInputForward();
		}

		//
		accept = accept.getAcceptInfoBySerial(serial, licensePlate, vin);
		if (accept != null) {
			view.setSerial(accept.getSerial());
			view.setLicensePlate(accept.getLicensePlate());
			view.setVin(accept.getVin());
		}
		Codes code = Codes.getInstance();
		List chargeTypeList = new ArrayList();
		if ("1".equals(inputForm.getSaveType())) chargeTypeList = code.getCodeslistByItemId(Codes.CHARGE_TYPE_CHARGE);
		else if ("2".equals(inputForm.getSaveType())) chargeTypeList = code.getCodeslistByItemId(Codes.CHARGE_TYPE_REFUND);
		if (chargeTypeList != null) view.setChargeTypeList(chargeTypeList);
		view.setSaveType(inputForm.getSaveType());// 保存业务类型
		
		if(inputForm.getTransferPriceChargeType() == 0) view.setTransferPriceChargeType(9);// 收费类型
		else view.setTransferPriceChargeType(inputForm.getTransferPriceChargeType());// 收费类型
		
		if ("1".equals(inputForm.getSaveType())) view.setType("收费");
		else if ("2".equals(inputForm.getSaveType())) view.setType("退费");
		TransferFinance transferFinance = new TransferFinance();
		List<TransferFinanceDetailDto> transferFinanceList = new ArrayList<TransferFinanceDetailDto>();
		if(accept!=null && StringUtil.isNotBlank(accept.getSerial())) transferFinanceList = transferFinance.getTransferFinanceList(accept.getSerial());
		view.setDetailList(transferFinanceList);
		request.setAttribute(TmsConstants.VIEWHELP, view);
		uc.setRequestAttribute(TmsConstants.VIEWHELP, view);

		return mapping.findForward(TmsConstants.SUCCESS);
	}
}
